package com.application.sistercityapplication.listadapter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.PowerManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.sistercityapplication.dto.InformationDto;
import com.application.sistercityapplication.dto.MagazineDto;
import com.application.sistercityapplication.listadapter.InformationDetailListAdapter.DownloadDocumentClickListener;
import com.application.sistercityapplication.main.R;
import com.application.sistercityapplication.model.Magazine;
import com.application.sistercityapplication.utils.DownloadTask;
import com.artifex.mupdfdemo.MuPDFActivity;
import com.roscopeco.ormdroid.Entity;
import com.squareup.picasso.Picasso;
import com.todddavies.components.progressbar.ProgressWheel;

public class LoadedDocumentListAdapter extends BaseAdapter{
	private List<MagazineDto> _list;
	private ViewHolder viewHolder;
	private Context _context;
	private Activity _activity;
	private int _deleteState;
	private ImageView _btnDel;
	private MagazineDto magazineObj;
	
	public LoadedDocumentListAdapter(List<MagazineDto> docList,Context con,Activity activity,int deleteState,ImageView btnDel) {
		// TODO Auto-generated constructor stub
		this._list = docList;
		this._context = con;
		this._activity = activity;
		this._deleteState = deleteState;
		this._btnDel = btnDel;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return _list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.document_list_each_item, null);
			viewHolder.documentThumbnail = (ImageView) convertView.findViewById(R.id.documentThumbnail);
			viewHolder.countryText = (TextView) convertView.findViewById(R.id.countryName);
			viewHolder.documentTitle = (TextView) convertView.findViewById(R.id.text_item);
			viewHolder.date = (TextView) convertView.findViewById(R.id.pubDate);
			//viewHolder.checkBox = (ImageView) convertView.findViewById(R.id.imageView1);
			viewHolder.downLoadBookBtn = (ImageView) convertView.findViewById(R.id.downLoadBookBtn);
			viewHolder.click_relative = (RelativeLayout) convertView.findViewById(R.id.click_relative);
//			if(_deleteState==99){
//				viewHolder.checkBox.setVisibility(View.VISIBLE);
//				viewHolder.checkBox.setImageDrawable(_activity.getResources().getDrawable(R.drawable.check_bok_full));
//			}else if(_deleteState==1){
//				viewHolder.checkBox.setVisibility(View.VISIBLE);
//				viewHolder.checkBox.setImageDrawable(_activity.getResources().getDrawable(R.drawable.check_bok_empty));
//				
//			}
//			else{
//				viewHolder.checkBox.setVisibility(View.GONE);
//			}
			convertView.setTag(viewHolder);
			
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		magazineObj = _list.get(position);
		try{
			
			Picasso.with(_context).load(magazineObj.getTumbnail()).into(viewHolder.documentThumbnail);
			viewHolder.countryText.setText(magazineObj.getCat());
			viewHolder.date.setText(magazineObj.getCreated_at());
			viewHolder.documentTitle.setText(magazineObj.getTitle());
			final Magazine mag = Entity.query(Magazine.class).where("magId").eq(magazineObj.getMagId()).execute();
			if(mag!=null){
				viewHolder.downLoadBookBtn.setVisibility(View.GONE);
				viewHolder.click_relative.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Uri uri = Uri.parse(mag.path);

						Intent intent = new Intent(_activity, MuPDFActivity.class);

						intent.setAction(Intent.ACTION_VIEW);

						intent.setData(uri);

						_activity.startActivity(intent);
					}
				});
			}
			viewHolder.downLoadBookBtn.setOnClickListener(new DownloadDocumentClickListener(position,convertView,magazineObj));

		}catch(Exception e){
			e.printStackTrace();
		}
		return convertView;
	}
	public class DownloadDocumentClickListener implements OnClickListener{
		private int _position;
		private View view;
		private MagazineDto magazineDto;
		public DownloadDocumentClickListener(int position,View btnView,MagazineDto magazineDto){
			this._position = position;
			this.view = btnView;
			this.magazineDto = magazineDto;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub'
			
			//Toast.makeText(_context, String.valueOf(_position), Toast.LENGTH_SHORT).show();
			//viewHolder.downLoadBookBtn.setImageDrawable(_activity.getResources().getDrawable(R.drawable.download_temp));
			ImageView downLoadBookBtn = (ImageView) view.findViewById(R.id.downLoadBookBtn);
			downLoadBookBtn.setVisibility(View.GONE);
			
			ProgressWheel progressWheel = (ProgressWheel) view.findViewById(R.id.pw_spinner);
			progressWheel.setVisibility(View.VISIBLE);
			DownloadTask downloadTask = new DownloadTask(_activity,progressWheel,magazineDto,LoadedDocumentListAdapter.this);
			downloadTask.execute(magazineDto.getPath());
			
		}
		
	}
	
	public class ViewHolder {
		private ImageView documentThumbnail,checkBox,downLoadBookBtn;;
		private TextView countryText,documentTitle,date;
		private RelativeLayout click_relative;
		
	}
}
